        TITL DAI FIRMWARE 2E71B-2E7FB  V1.1
        ORG   :E71B
*****************************
* DRAW A LINE ON THE SCREEN *
*****************************
*
* Draws a line in a given colour between two
* arbitrary points on the screen.
*
* The coordinates are given inclusively. The
* line will be drawn starting at the left end,
* whichever order the parameters are given in.
*
* Entry: B,DE: Y,X coordinate of one end of the
*              line.
*        C,HL: Idem of the other end.
*        A:    Colour of the line.
* Exit:  CY=0: O.K.
*        CY=1: Errorcode in A.
*        ABCDEHL preserved.
*
SDRAW   ORA   A
        PUSH  PSW
        PUSH  B
        PUSH  D
        PUSH  H
        CALL  ARGCHK    Check arguments, set colour
        PUSH  PSW
        CALL  COMP      Check direction of line
        MVI   A,:00     Set 'no X,Y swop'
        JNC   DRL30     Jump if X > Y
 REM
* Swop X,Y:
 REM
        XCHG            Exchange X coordinates
        CMA
*
DRL30   STA   GRCOL     FF if X,Y swop, else 00
        MOV   A,C
        ANI   :07
        MOV   D,A       Offset in field in D
        POP   PSW       Get Y-pos left end
        PUSH  H         Save X length
        CALL  SMEMMK    Pntr to start of line in
 REM screen RAM
        XTHL
        PUSH  D         Save offset
        PUSH  H         Save DX
        CALL  CMPHL     HL = - DX
        XTHL
        PUSH  H         Save DX
        MOV   L,E
        MVI   H,:00
        DAD   H
        SHLD  COR       Store 2*DY (adj long
 REM sectors)
        MOV   A,E       DY in A
        STA   MODFLG    Set count of sectors (1-256)
        POP   H
        CALL  HLDIV     HL = DX / DY
        SHLD  SECT      SECT is INT(DX/DY)
        POP   B         Get -DX
        PUSH  H         Save SECT
        MOV   A,E
        CALL  HLMUL     HL = SECT*DY
        DAD   B         HL = SECT*DY-DX
        DAD   H         HL = 2*(SECT*DY-DX)
        SHLD  DELTA     Store amount to add into
 REM count
        POP   H         Get SECT
        MOV   A,H
        RAR
        MOV   A,L
        RAR             A=INT(SECT/2)
        STA   COLMOD    Store amount to trim off
 REM last sector
        INR   A
        MOV   L,A
        MVI   H,:00     HL = INIT
        PUSH  H
        DAD   H         HL = 2*INIT
        MOV   A,E
        CALL  HLMUL     HL = 2*INIT*DY
        DAD   B         HL = 2*INIT*DY-DX
        SHLD  RT        Set INIT running total
        POP   H         Get length 1st sector
        POP   PSW
        MOV   C,A       C is initial offset
        MOV   A,E
        ORA   A
        JNZ   DRL40     If more than 1 sector
        STA   COLMOD    Store amount to trim off
 REM last sector
        LHLD  SECT      Get lower of 2 possible
 REM sectors
        INX   H         Prig length if only 1 sector
DRL40   LXI   D,MODFLG  Addr of nr of sectors
        LDAX  D         Get count of sectors
        SUI   :01       -1
        STAX  D         Store it again
        JNC   DRL50     Jump if not last sector
 REM
* Trim off last sector:
 REM
        LDA   COLMOD    Get amount to trim off
 REM last sector
        CMA
        MOV   E,A
        MVI   D,:FF
        INX   D
        DAD   D
*
DRL50   LXI   D,:0001   Init Y-size is 1
        LDA   GRCOL     ) Check if swop X,Y dir.
        ORA   A         ) (line > 45 degrees)
        JZ    DRL60     Jump if not
        XCHG            Swop X,Y direction
DRL60   MOV   B,E       Get Y-size in B
        XCHG            X-size in DE
        POP   H         Get memory pointer
        DCR   B
        LDA   GRMODE    ) Check for Y-invert
        ORA   A         )
        PUSH  PSW       Save condition
        CNZ   UPDTP     Move pntr to bottom of
 REM sector
        DCX   D         Interfacing
        CALL  FILRT     Draw next sector of line
        INX   D         ) Re-instate real values
        INR   B         )
        POP   PSW       Get earlier condition
        JZ    DRL80     Jump if no Y-invert
        MVI   B,:01     Init 1 blob down only
DRL80   CALL  UPDTP     Move ptr up/down
        MOV   A,C       Get offset
        ADD   E         Add X-movement
        MOV   C,A       ) Save result
        MOV   B,A       )
        ANI   :07
        CMP   C
        JZ    DRL83     Jump if not new field
 REM
* If new field:
 REM
        MOV   C,A       Update offset
        MOV   A,B       Get complement new offset
        XRA   C         Clip bits 0,1,2 off
        RAR             )
        RAR             )
        CMA             ) Update pointer
        MOV   E,A       ) to new field
        MVI   D,:FF     )
        INX   D         )
        DAD   D         )
*
DRL83   PUSH  H         Save memory pointer
        LHLD  DELTA     Get amount to add into count
        XCHG            in DE
        LHLD  RT        Get count running total
        DAD   D         Add up
        XCHG            Result in DE
        LHLD  SECT      Get lowest of 2 possible
 REM sectors
        XCHG            in DE (distance to go)
        MOV   A,H
        ORA   A
        JP    DRL86     Jump if short sector
 REM
* If long sector:
 REM
        INX   D         Go one blob further
        PUSH  D
        XCHG
        LHLD  COR       Get adjustment for long
 REM sectors
        DAD   D         Adjust error term
        POP   D
DRL86   SHLD  RT        Update running total
        XCHG
        LDA   MODFLG    Get nr of sectors
        INR   A
        JNZ   DRL40     Next sector if not ready
 REM
* If ready:
 REM
        POP   H
        JMP   XRET      Popall, ret
*
*
*
        UNL
ARGCHK  EQU   :E83A
COMP    EQU   :E6FB
GRCOL   EQU   :00C0
SMEMMK  EQU   :EBB9
CMPHL   EQU   :E706
COR     EQU   :00B9
MODFLG  EQU   :00BD
HLDIV   EQU   :EB60
SECT    EQU   :00BB
HLMUL   EQU   :EB46
DELTA   EQU   :00B5
COLMOD  EQU   :00BE
RT      EQU   :00B7
GRMODE  EQU   :00BF
UPDTP   EQU   :E7FC
FILRT   EQU   :EAF7
XRET    EQU   :E138
        LST
        END
